package firstPack;

import static org.apache.commons.io.FileUtils.readLines;
import static org.apache.commons.io.FileUtils.writeStringToFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;

/**
 * Runs the main menu and controls all the static variables that the
 * other classes/states need. Also controls the reading of the high
 * scores from a file and some helpful methods to organize those
 * scores. It writes the high scores to a file before the program
 * exits.
 * State 0
 * @author El Jefe
 *
 */
public class GameState extends BasicGameState{
	
	public static int totalScore;
	public static int lives;
	public static int minHighScore;
	public static HashMap<Integer, String> highScores;

	@Override
	public void init(GameContainer arg0, StateBasedGame arg1)
			throws SlickException {
		highScores = new HashMap<Integer,String>();
		highScores.put(0, "Noob");
		try {
			ArrayList<String> fileLines = (ArrayList<String>) readLines(new File("HScore.txt"));
			highScores.remove(0);
			for (String lines : fileLines){
				String[] splitL = lines.split(",");
				highScores.put(Integer.parseInt(splitL[1]), splitL[0]);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("High Score can't be found");
			minHighScore = 0;
		}
		updateMinScore();
		
	}

	@Override
	public void render(GameContainer arg0, StateBasedGame arg1, Graphics disp)
			throws SlickException {
		String mainDisplay = "Welcome to the slick2D game";
		mainDisplay += "\n\nWhat would you like to do?\n";
		mainDisplay += "1 : Play the game\n2 : See High Scores\n3 : Credits\n4 : Exit\n";// + minHighScore;
		disp.drawString(mainDisplay, 50, 50);
		
	}

	@Override
	public void update(GameContainer container, StateBasedGame sbg, int arg2)
			throws SlickException {
		Input input = container.getInput();
		if(input.isKeyPressed(input.KEY_1)){
			input.clearKeyPressedRecord();
			sbg.enterState(1, new FadeOutTransition(), new FadeInTransition());
		}
		
		if(input.isKeyPressed(input.KEY_2)){
			sbg.enterState(4, new FadeOutTransition(), new FadeInTransition());
		}
		
		if(input.isKeyPressed(input.KEY_3)){
			sbg.enterState(5, new FadeOutTransition(), new FadeInTransition());
		}
		
		if(input.isKeyPressed(input.KEY_4)){
			ArrayList<Integer> sortMe = sortScores();
			String toPrint = "";
			for(Integer value : sortMe){
				toPrint += highScores.get(value) + "," + value + "\n";
			}
			try {
				writeStringToFile(new File("HScore.txt"), toPrint);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			container.exit();
		}
		
		
	}

	@Override
	public int getID() {
		// TODO Auto-generated method stub
		return 0;
	}
	
	public static ArrayList<Integer> sortScores(){
		ArrayList<Integer> sortedScores = new ArrayList<Integer>();
		for(Integer keys : highScores.keySet()){
			sortedScores.add(keys);
		}
		
		int temp, j;
		boolean stopMe = true;
		for(int i = 1; i < highScores.size(); i++){
			j = i;
			while (stopMe){
				if(sortedScores.get(j) > sortedScores.get(j - 1)){
					temp = sortedScores.get(j);
					sortedScores.set(j, sortedScores.get(j - 1));
					sortedScores.set(j - 1, temp);
					j--;
					if(j == 0){
						stopMe = false;
					}
				}
				else{
					stopMe = false;
				}
			}
			stopMe = true;
		}
		
		return sortedScores;
	}
	
	public static void updateMinScore(){
		minHighScore = 9000;
		for(Integer keys : highScores.keySet()){
//			System.out.println(keys + " " + highScores.get(keys));
			if(minHighScore > keys){
				minHighScore = keys;
			}
		}
		if (highScores.size() < 10){
			minHighScore = 0;
		}
	}
	
	
	
	

}
